From fdd179a340d6f515f392599de2fd787a25f40d7e Mon Sep 17 00:00:00 2001 From: Joshua DeSeno Date: Thu, 24 Jul 2014 10:54:28 +0900 Subject: [PATCH] Replace duplicate code for loading project manifest --- src/bin/cargo-build.rs | 12 ++---------- src/bin/cargo-clean.rs | 12 ++---------- src/bin/cargo-run.rs | 13 ++----------- src/bin/cargo-test.rs | 13 ++----------- src/cargo/util/important_paths.rs | 15 ++++++++++++++- 5 files changed, 22 insertions(+), 43 deletions(-) diff --git a/src/bin/cargo-build.rs b/src/bin/cargo-build.rs index 81c510212..349a12621 100644 --- a/src/bin/cargo-build.rs +++ b/src/bin/cargo-build.rs @@ -17,7 +17,7 @@ use cargo::ops; use cargo::ops::CompileOptions; use cargo::core::MultiShell; use cargo::util::{CliResult, CliError}; -use cargo::util::important_paths::find_project_manifest; +use cargo::util::important_paths::{find_root_manifest_for_cwd}; #[deriving(PartialEq,Clone,Decodable,Encodable)] pub struct Options { @@ -40,15 +40,7 @@ fn main() { fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { debug!("executing; cmd=cargo-compile; args={}", os::args()); - let root = match options.manifest_path { - Some(path) => Path::new(path), - None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml") - .map_err(|_| { - CliError::new("Could not find Cargo.toml in this \ - directory or any parent directory", - 102) - })) - }; + let root = try!(find_root_manifest_for_cwd(options.manifest_path)); let env = if options.release { "release" diff --git a/src/bin/cargo-clean.rs b/src/bin/cargo-clean.rs index 4bd1e7484..dbe597830 100644 --- a/src/bin/cargo-clean.rs +++ b/src/bin/cargo-clean.rs @@ -16,7 +16,7 @@ use cargo::ops; use cargo::{execute_main_without_stdin}; use cargo::core::MultiShell; use cargo::util::{CliResult, CliError}; -use cargo::util::important_paths::find_project_manifest; +use cargo::util::important_paths::{find_root_manifest_for_cwd}; #[deriving(PartialEq,Clone,Decodable,Encodable)] pub struct Options { @@ -32,15 +32,7 @@ fn main() { fn execute(options: Options, _shell: &mut MultiShell) -> CliResult> { debug!("executing; cmd=cargo-clean; args={}", os::args()); - let root = match options.manifest_path { - Some(path) => Path::new(path), - None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml") - .map_err(|_| { - CliError::new("Could not find Cargo.toml in this \ - directory or any parent directory", - 102) - })) - }; + let root = try!(find_root_manifest_for_cwd(options.manifest_path)); ops::clean(&root).map(|_| None).map_err(|err| { CliError::from_boxed(err, 101) diff --git a/src/bin/cargo-run.rs b/src/bin/cargo-run.rs index 216d783da..024b5df26 100644 --- a/src/bin/cargo-run.rs +++ b/src/bin/cargo-run.rs @@ -8,14 +8,13 @@ extern crate serialize; #[phase(plugin, link)] extern crate hammer; -use std::os; use std::io::process::ExitStatus; use cargo::ops; use cargo::{execute_main_without_stdin}; use cargo::core::{MultiShell}; use cargo::util::{CliResult, CliError}; -use cargo::util::important_paths::find_project_manifest; +use cargo::util::important_paths::{find_root_manifest_for_cwd}; #[deriving(PartialEq,Clone,Decodable)] struct Options { @@ -34,15 +33,7 @@ fn main() { } fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { - let root = match options.manifest_path { - Some(path) => Path::new(path), - None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml") - .map_err(|_| { - CliError::new("Could not find Cargo.toml in this \ - directory or any parent directory", - 102) - })) - }; + let root = try!(find_root_manifest_for_cwd(options.manifest_path)); let mut compile_opts = ops::CompileOptions { update: options.update, diff --git a/src/bin/cargo-test.rs b/src/bin/cargo-test.rs index 1337b57b8..6dccc9aaa 100644 --- a/src/bin/cargo-test.rs +++ b/src/bin/cargo-test.rs @@ -8,7 +8,6 @@ extern crate serialize; #[phase(plugin, link)] extern crate hammer; -use std::os; use std::io::process::ExitStatus; use cargo::ops; @@ -16,7 +15,7 @@ use cargo::{execute_main_without_stdin}; use cargo::core::{MultiShell}; use cargo::util; use cargo::util::{CliResult, CliError, CargoError}; -use cargo::util::important_paths::find_project_manifest; +use cargo::util::important_paths::{find_root_manifest_for_cwd}; #[deriving(PartialEq,Clone,Decodable)] struct Options { @@ -35,15 +34,7 @@ fn main() { } fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { - let root = match options.manifest_path { - Some(path) => Path::new(path), - None => try!(find_project_manifest(&os::getcwd(), "Cargo.toml") - .map_err(|_| { - CliError::new("Could not find Cargo.toml in this \ - directory or any parent directory", - 102) - })) - }; + let root = try!(find_root_manifest_for_cwd(options.manifest_path)); let mut compile_opts = ops::CompileOptions { update: options.update, diff --git a/src/cargo/util/important_paths.rs b/src/cargo/util/important_paths.rs index df7a6bd7a..614bfd307 100644 --- a/src/cargo/util/important_paths.rs +++ b/src/cargo/util/important_paths.rs @@ -1,4 +1,5 @@ -use util::{CargoResult, human}; +use std::os::{getcwd}; +use util::{CargoResult, CliError, CliResult, human}; /// Iteratively search for `file` in `pwd` and its parents, returning /// the path of the directory. @@ -29,6 +30,18 @@ pub fn find_project_manifest(pwd: &Path, file: &str) -> CargoResult { file, pwd.display()))) } +/// Find the root Cargo.toml +pub fn find_root_manifest_for_cwd(manifest_path: Option) -> CliResult { + match manifest_path { + Some(path) => Ok(Path::new(path)), + None => match find_project_manifest(&getcwd(), "Cargo.toml") { + Ok(x) => Ok(x), + Err(_) => Err(CliError::new("Could not find Cargo.toml in this \ + directory or any parent directory", 102)) + } + } +} + /// Return the path to the `file` in `pwd`, if it exists. pub fn find_project_manifest_exact(pwd: &Path, file: &str) -> CargoResult { let manifest = pwd.join(file); -- 2.30.2